<template>
  <div class="app-container">
    <!-- 表格 -->
    <el-table :data="dataList" element-loading-text="数据加载中" fit highlight-current-row
              row-class-name="myClassList"
              border>
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column prop="title" label="课程名称"></el-table-column>
      <el-table-column label="课程封面" align="center">
        <template slot-scope="scope">
          <div class="pic">
            <img :src="scope.row.cover" alt="scope.row.title" height="150px">
          </div>
        </template>
      </el-table-column>

      <el-table-column label="发布时间" width="160px">
        <template slot-scope="scope">{{ scope.row.updateTime}}</template>
      </el-table-column>
      <el-table-column label="价格">
        <template slot-scope="scope">
          {{ Number(scope.row.price) === 0 ? '免费' : '¥' + scope.row.price.toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column prop="buyCount" label="付费学员">
        <template slot-scope="scope">{{ scope.row.buyCount }}人</template>
      </el-table-column>

      <!--
          js 有数据类型  6个
          ==  是比较的内容
               (Number) 6 == "6" (string)  比较内容 会进行转换  true

          === 是比较数据类型 和 内容
              (Number) 6 === "6" (string)  比较类型再内容        false
      -->
      <el-table-column prop="status" label="课程状态" align="center">

        <template slot-scope="scope">
          <el-tag
            :type="scope.row.status === 'Normal' ? 'success' : 'warning'"
            disable-transitions>{{scope.row.status === 'Normal'? "已发布":"未发布课程"}}
          </el-tag>
        </template>

      </el-table-column>

      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="text" size="mini" icon="el-icon-edit" @click="toEditCourse(scope.row.id)">编辑课程信息</el-button>
          <el-button type="text" size="mini" icon="el-icon-edit" @click="toEditChapter(scope.row.id)">编辑课程大纲</el-button>
          <el-button type="text" size="mini" icon="el-icon-delete">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[3, 6]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

  </div>
</template>

<script>

  import course from "@/api/course";

  export default {
    name: "index",
    data() {
      return {
        dataList: [],
        currentPage: 1,
        pageSize: 3,
        total: 0
      }
    },
    methods: {

      toEditChapter(id) {
        //路由跳转 大纲页面
        this.$router.push({path: "/course/chapter/" + id})
      },

      //跳转到编辑页面
      toEditCourse(id) {
        //路由跳转 编辑页面
        this.$router.push({path: "/course/edit/" + id})
      },
      handleSizeChange(val) {
        this.pageSize = val;
        this.initData();
      },

      handleCurrentChange(val) {
        this.currentPage = val;
        this.initData();
      },

      initData() {
        course.findCourse(this.currentPage, this.pageSize).then(resp => {
          this.dataList = resp.data.data;
          this.total = resp.data.total;
        })
      }
    },
    created() {

      this.initData();

    }
  }
</script>

<style scoped>

</style>
